home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / sat / msat09.tgz / HEADER.C < prev    next >
Text File  |  1994-09-17  |  7KB  |  264 lines

  1. /*
  2.  *   Copyright 1992, 1993, 1994 John Melton (G0ORX/N6LYT)
  3.  *              All Rights Reserved
  4.  *
  5.  *   This program is free software; you can redistribute it and/or modify
  6.  *   it under the terms of the GNU General Public License as published by
  7.  *   the Free Software Foundation; either version 1, or (at your option)
  8.  *   any later version.
  9.  *
  10.  *   This program is distributed in the hope that it will be useful,
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *   GNU General Public License for more details.
  14.  *
  15.  *   You should have received a copy of the GNU General Public License
  16.  *   along with this program; if not, write to the Free Software
  17.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20.  
  21. /*
  22.     header.c
  23.  
  24.     Extract an FTL0 header from a file.
  25.  
  26.     John Melton
  27.  
  28.     G0ORX, N6LYT
  29.  
  30.     4 Charlwoods Close
  31.     Copthorne
  32.     West Sussex
  33.     RH10 3QZ
  34.     England
  35.  
  36.     Note: there may be a better way to do this, rather than having
  37.     fixed field sizes in a structure.
  38. */
  39.  
  40. #include <X11/Intrinsic.h>
  41.  
  42. #include <stdio.h>
  43. #include <time.h>
  44.  
  45. #include "ftl0.h"
  46. #include "header.h"
  47.  
  48. void HeaderCopy(char *source, int length,
  49.         char *destination, int maxbytes);
  50. void ConvertTime(long *pTime);
  51.  
  52. HEADER *ExtractHeader(unsigned char *buffer, int nBytes, int *size)
  53. {
  54.     int i = 0;
  55.     int bMore = 0;
  56.     unsigned id;
  57.     unsigned char length;
  58.     HEADER  *hdr;
  59.  
  60.     if ((hdr = (HEADER *)XtMalloc(sizeof(HEADER))) != NULL)
  61.     {
  62.         hdr->fileId             = 0;
  63.         hdr->fileName[0]        = '\0';  
  64.         hdr->fileExt[0]         = '\0';   
  65.         hdr->fileSize           = 0;     
  66.         hdr->createTime         = 0;   
  67.         hdr->modifiedTime       = 0; 
  68.         hdr->SEUflag            = 0;      
  69.         hdr->fileType           = 0;     
  70.         hdr->bodyCRC            = 0;      
  71.         hdr->headerCRC          = 0;    
  72.         hdr->bodyOffset         = 0;   
  73.  
  74.         hdr->source[0]          = '\0';         
  75.         hdr->uploader[0]        = '\0';        
  76.         hdr->uploadTime         = 0;         
  77.         hdr->downloadCount      = 0;      
  78.         hdr->destination[0]     = '\0';    
  79.         hdr->downloader[0]      = '\0';      
  80.         hdr->downloadTime       = 0;       
  81.         hdr->expireTime         = 0;         
  82.         hdr->priority           = 0;           
  83.         hdr->compression        = 0;        
  84.         hdr->BBSMessageType     = ' ';     
  85.         hdr->BID[0]             = '\0';            
  86.         hdr->title[0]           = '\0';          
  87.         hdr->keyWords[0]        = '\0';       
  88.         hdr->description[0]     = '\0';    
  89.         hdr->compressionDesc[0] = '\0';
  90.         hdr->userFileName[0]    = '\0';   
  91.  
  92.         if (buffer[0] != 0xAA || buffer[1] != 0x55)
  93.         {
  94.             XtFree((char *)hdr);
  95.             return (HEADER *)0;
  96.         }
  97.  
  98.         bMore = 1;
  99.  
  100.         i = 2; /* skip over 0xAA 0x55 */
  101.  
  102.         while (bMore && i < nBytes)
  103.         {
  104.             id = buffer[i++];
  105.             id += buffer[i++] << 8;
  106.             length = buffer[i++];
  107.  
  108. #ifdef DEBUG
  109. printf("ExtractHeader: id:%X length:%d\n", id, length);
  110. #endif
  111.             switch (id)
  112.             {
  113.             case 0x00:
  114.                 bMore = 0;
  115.                 break;
  116.             case 0x01:
  117.                 hdr->fileId = *(unsigned long *)&buffer[i];
  118.                 break;
  119.             case 0x02:
  120.                 HeaderCopy(&buffer[i], length, hdr->fileName, 9);
  121.                 break;
  122.             case 0x03:
  123.                 HeaderCopy(&buffer[i], length, hdr->fileExt, 3);
  124.                 break;
  125.             case 0x04:
  126.                 hdr->fileSize = *(unsigned long *)&buffer[i];
  127.                 break;
  128.             case 0x05:
  129.                 hdr->createTime = *(unsigned long *)&buffer[i];
  130.                        ConvertTime(&hdr->createTime);
  131.                 break;
  132.             case 0x06:
  133.                 hdr->modifiedTime = *(unsigned long *)&buffer[i];
  134.                        ConvertTime(&hdr->modifiedTime);
  135.                 break;
  136.             case 0x07:
  137.                 hdr->SEUflag = buffer[i];
  138.                 break;
  139.             case 0x08:
  140.                 hdr->fileType = buffer[i];
  141.                 break;
  142.             case 0x09:
  143.                 hdr->bodyCRC = *(unsigned short *)&buffer[i];
  144.                 break;
  145.             case 0x0A:
  146.                 hdr->headerCRC = *(unsigned short *)&buffer[i];
  147.                 break;
  148.             case 0x0B:
  149.                 hdr->bodyOffset = *(unsigned short *)&buffer[i];
  150.                 break;
  151.             case 0x10:
  152.                 HeaderCopy(&buffer[i], length, hdr->source, 32);
  153.                 break;
  154.             case 0x11:
  155.                 HeaderCopy(&buffer[i], length, hdr->uploader, 6);
  156.                 break;
  157.             case 0x12:
  158.                 hdr->uploadTime = *(unsigned long *)&buffer[i];
  159.                 ConvertTime(&hdr->uploadTime);
  160.                 break;
  161.             case 0x13:
  162.                 hdr->downloadCount = buffer[i];
  163.                 break;
  164.             case 0x14:
  165.                 HeaderCopy(&buffer[i], length, hdr->destination, 32);
  166.                 break;
  167.             case 0x15:
  168.                 HeaderCopy(&buffer[i], length, hdr->downloader, 6);
  169.                 break;
  170.             case 0x16:
  171.                 hdr->downloadTime = *(unsigned long *)&buffer[i];
  172.                 ConvertTime(&hdr->downloadTime);
  173.                 break;
  174.             case 0x17:
  175.                 hdr->expireTime = *(unsigned long *)&buffer[i];
  176.                 ConvertTime(&hdr->expireTime);
  177.                 break;
  178.             case 0x18:
  179.                 hdr->priority = buffer[i];
  180.                 break;
  181.             case 0x19:
  182.                 hdr->compression = buffer[i];
  183.                 break;
  184.             case 0x20:
  185.                 hdr->BBSMessageType = buffer[i];
  186.                 break;
  187.             case 0x21:
  188.                 HeaderCopy(&buffer[i], length, hdr->BID, 32);
  189.                 break;
  190.             case 0x22:
  191.                 HeaderCopy(&buffer[i], length, hdr->title, 64);
  192.                 break;
  193.             case 0x23:
  194.                 HeaderCopy(&buffer[i], length, hdr->keyWords, 32);
  195.                 break;
  196.             case 0x24:
  197.                 HeaderCopy(&buffer[i], length, hdr->description, 32);
  198.                 break;
  199.             case 0x25:
  200.                 HeaderCopy(&buffer[i], length, hdr->compressionDesc, 32);
  201.                 break;
  202.             case 0x26:
  203.                 HeaderCopy(&buffer[i], length, hdr->userFileName, 32);
  204.                 break;
  205.             default:
  206.                 printf("Unknown header item, id:%d length:%d data:%02X %02X %02X %02X\n",
  207.                     id, length, buffer[i], buffer[i+1], buffer[i+2], buffer[i+3]);
  208.                     break;
  209.             }
  210.  
  211.             i+=length;
  212.         }
  213.      }
  214.  
  215.     /* let the user know the size */
  216.     *size = i;
  217.  
  218.     /* see if we ran out of space */
  219.     if (bMore)
  220.     {
  221.         XtFree((char *)hdr);
  222.         return NULL;
  223.     }
  224.  
  225.     return hdr;
  226. }
  227.  
  228. void HeaderCopy(char *source, int length, char *destination, int maxbytes)
  229. {
  230.     if (length > maxbytes) length = maxbytes;
  231.  
  232.     while (length > 0)
  233.     {
  234.         *destination++ = *source++;
  235.         length--;
  236.     }
  237.     
  238.     *destination = '\0';
  239. }
  240.  
  241. /*
  242.   ConvertTime
  243.  
  244.   Time is number of seconds since Jan 1 1970
  245.   MicroSoft wants it to be since 1900
  246. */
  247. void ConvertTime(long * pTime)
  248. {
  249.     struct tm time70;
  250.   
  251.     /* time is number of seconds since 1970 (MSC wants since 1900) */
  252.     time70.tm_sec   = 0;
  253.     time70.tm_min   = 0;
  254.     time70.tm_hour  = 0;
  255.     time70.tm_mday  = 1;
  256.     time70.tm_mon   = 0;
  257.     time70.tm_year  = 70;
  258.       time70.tm_wday  = 0;
  259.     time70.tm_yday  = 0;
  260.     time70.tm_isdst = 0;
  261.  
  262.     *pTime += (long)mktime(&time70);
  263. }
  264.